package com.zhiwang.xiaoqu.service

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import com.zhiwang.xiaoqu.util.AppDetector
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class AppUsageService : Service() {

    val TAG = "AppUsageService"

    companion object SOURL {

        fun startDetectApp(context: Context, packageName: String, time: Int) {
            val intent = Intent(context, AppUsageService::class.java)

            intent.putExtra("Time", time)
            intent.putExtra("PKG_NAME", packageName)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(intent)
            }
        }
    }

    override fun onBind(intent: Intent?) = null


    var targetPackageName: String = ""

    var timePassed = 0

    var targetUsageTime = 0

    var disposable: Disposable? = null

    var targetFound = false

    override fun onCreate() {
        super.onCreate()

        createForeGroundNotification()
    }

    private fun createForeGroundNotification() {

    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        intent?.apply {
            val pkgName = intent.getStringExtra("PKG_NAME")
            val time = intent.getIntExtra("Time", 0)
            if (pkgName.isNotEmpty() && time > 0 && pkgName != targetPackageName) {
                continuouslyDetect(pkgName, time)
            }
        }

        return Service.START_STICKY
    }

    private fun continuouslyDetect(pkgName: String, time: Int) {
        reset()
        targetUsageTime = time
        disposable = Flowable.interval(2, TimeUnit.SECONDS)
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({

                Log.i(TAG, "continuouslyDetect: ----------tick---------------")

                val found = AppDetector.queryAppUsage(this, pkgName)

                if (targetFound && found)
                    timePassed += 2

                if (targetFound && found.not()) {
                    //提示继续玩
                }

                if (timePassed > targetUsageTime) {
                    //
                    Log.i(TAG, "continuouslyDetect: 领取奖励-------------------------------------")
                    stopSelf()
                }

                if (targetFound.not() && found) {
                    Log.i(TAG, "continuouslyDetect: ----Found App--")
                }

                targetFound = found

            }, {

            }, {

            })
    }


    fun reset() {
        disposable?.dispose()
        targetFound = false
        timePassed = 0
    }

    override fun onDestroy() {
        super.onDestroy()
        disposable?.dispose()
    }

}